/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

import enteties.Album;
import enteties.Image;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Trung Kien
 */
public class UploadImage {

    private String url;
    private String user;
    private String password;

    public UploadImage(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public boolean CreatImage(Image i) {
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call creatIamge(?, ?, ?, ?, ?)}");
            cstmt.setString(1, i.getImgName());
            cstmt.setString(2, i.getDatshooting());
            cstmt.setInt(3, i.getUserid());
            cstmt.setInt(4, i.getAlbumid());
            cstmt.setString(5, i.getIntroduction());
            int rows = cstmt.executeUpdate();
            conn.close();
            return rows > 0;
        } catch (SQLException ex) {
            Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean CreateAlbum(Album a){
        try {
            Connection conn =  ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call CreateAlbum(?,?,?,?)}");
            cstmt.setString(1, a.getAlbumname());
            cstmt.setString(2, a.getDescription());
            cstmt.setString(3, a.getDatecreate());
            cstmt.setString(4, a.getUsername());
            int rows = cstmt.executeUpdate();
            conn.close();
            return rows >0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
   public List<Image> getImage(){
       List<Image> listImage = new ArrayList();
       try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call getImage()}");
            ResultSet rs = cstmt.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String imgName = rs.getString(2);
                String dayshooting = rs.getString(3);
                int userid = rs.getInt(4);
                int albumid = rs.getInt(5);
                String introduction = rs.getString(6);
                
                Image i = new Image(id, imgName, dayshooting, userid, albumid, introduction);
                listImage.add(i);
               
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listImage;
   }
}
